package com.zys.sac.core.handler;

import com.zys.sac.core.exception.SacAuthorizationException;
import com.zys.sac.core.util.ServletResponseUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 访问拒绝处理器，当要访问的url不在权限范围内会调用该处理器
 *
 * Created by zhongjunkai on 2022/3/11.
 */
public class SacAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException {
        SacAuthorizationException exception = (SacAuthorizationException)e;
        ServletResponseUtils.render(httpServletResponse, ServletResponseUtils.result(exception.getCode(), exception.getMessage()));
    }
}
